package com.ypc.webdemo.controller;

import com.ypc.webdemo.common.PluginActionEnum;
import com.ypc.webdemo.controller.dto.PluginInDto;
import com.ypc.webdemo.controller.dto.ServerResult;
import com.ypc.webdemo.dao.entity.Plugin;
import com.ypc.webdemo.service.PluginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author yinpengcheng
 * @Date 2023/7/21
 */
@Api(tags = "插件")
@RestController
@RequestMapping("plugins")
public class PluginController {
    @Resource
    private PluginService pluginService;

    @ApiOperation("上传插件")
    @PostMapping(value = "upload")
    public ServerResult<Plugin> upload(@RequestParam(value = "file") MultipartFile file) {
        return ServerResult.success(pluginService.upload(file));
    }

    @ApiOperation("查询插件列表")
    @GetMapping
    public ServerResult<ServerResult.PageDTO<Plugin>> findForPage(PluginInDto inDto) {
        return ServerResult.page(pluginService.findForPage(inDto));
    }

    @ApiOperation("插件安装和卸载")
    @PostMapping("{id}/action/{action}")
    public ServerResult action(@PathVariable String id, @PathVariable PluginActionEnum action) {
        pluginService.action(id, action);
        return ServerResult.success();
    }

    @ApiOperation("删除插件")
    @DeleteMapping("{id}")
    public ServerResult delete(@PathVariable String id) {
        pluginService.delete(id);
        return ServerResult.success();
    }
}
